import numpy as np
import load as ld
import curveLength as cur
import integration as integ
import matplotlib.pyplot as mp
import splines as sp

def y(f,Lambda,hmax):
	
	return lambda x : (1-Lambda)*f(x) + Lambda * 3 * hmax

def yPrime(fPrime,Lambda):

	return lambda x :  (1-Lambda)*fPrime(x)



def valeursCourbe(f,n,t):
    valeurs = range(n)
    for i in range(n):
        valeurs[i] = f(t[i])
    return valeurs



def ecoulement():

	ex = ld.ex
	ey = ld.ey
	ix = ld.ix
	iy = ld.iy
	
	f = sp.cubicSplineInterpolation(ex, ey)
	f2 = sp.cubicSplineInterpolation(ix, iy)
	
	t = np.arange(0.0, 1.0, 0.01)
	Lambda = np.arange(0.0,1.0,0.1)  
	
	for i in range(len(Lambda)):
		fi = y(f2, Lambda[i], min(iy))
		fe = y(f, Lambda[i], max(ey))
		ffini = valeursCourbe(fi,len(t),t)
		ffine = valeursCourbe(fe,len(t),t)
		mp.plot(t,ffini,linewidth=1.0)
		mp.plot(t,ffine,linewidth=1.0)

	mp.xlabel('x')
	mp.ylabel('y')
	mp.title("Ecoulement laminaire de part et d'autre de l'aile")
	mp.savefig("../img/ecoulement.png")
	mp.show()
	mp.clf()



def pressureOnCurve(Ps, length):
    """ Returns the pressure on a curve. Ps : static pressure, length : length of the curve """
    r = 1
    v = length/1
    return Ps + 0.5 * r * v**2


def PressureColour(P, Pbase, Pmax):
    """ Converts pressure to hexadecimal colour value """ 
    return ((P-Pbase)*10./Pmax)**3


def mapOfPressure(file_src, file_dest, integration_method, precision, n):
	
	
	ex = ld.ex
	ey = ld.ey
	ix = ld.ix
	iy = ld.iy
	fi = sp.cubicSplineInterpolation(ix, iy)
	fe = sp.cubicSplineInterpolation(ex, ey)
	fid = sp.cubicSplineInterpolationDerivative(ix, iy)
	fed = sp.cubicSplineInterpolationDerivative(ex, ey)
	

	t = np.arange(0.0, 1.0, 0.01)
	Pmax = 1
	Ps = 0
	
	fLambdaIPrime = yPrime(fid, 0)
	length_int =cur.curveLength( integration_method, fLambdaIPrime, min(ix), max(ix), n)
	Pbase = 0.9*pressureOnCurve(Ps, length_int)
    
	mp.clf()
	print "..."	    
	for l in np.arange(0.0,1.0,0.01):
		f_lambda_int =y(fi, l, min(iy))
		fLambdaIPrime = yPrime(fid, l)
		length_int = cur.curveLength(integration_method, fLambdaIPrime, min(ix), max(ix), n)
		pressure = pressureOnCurve(Ps, length_int)
		color = PressureColour(pressure, Pbase, Pmax)
		f_lambda_values_int = valeursCourbe(f_lambda_int,len(t),t)
		mp.plot(t,f_lambda_values_int,linewidth=5.0, color=(color, color**3,0))
	print "..."	
	for l in reversed( np.arange(0.0,1.0,0.01)):
		f_lambda_ext = y(fe, l, max(ey))
		f_lambda_ext_d = yPrime(fed, l)
		length_ext = cur.curveLength( integration_method, f_lambda_ext_d,min(ex), max(ex), n)
		pressure = pressureOnCurve(Ps, length_ext)
		color = PressureColour(pressure, Pbase, Pmax)
		f_lambda_values_ext = valeursCourbe(f_lambda_ext,len(t),t)
		mp.plot(t,f_lambda_values_ext,linewidth=5.0, color=(color,color**3,0))
	print "..."			
	mp.axis([min(ex), max(max(ex),max(ix)), 3*min(iy), 3*max(ey)])
	mp.xlabel('x')
	mp.ylabel('y')
	mp.title('Map pressure of the e327 (the lighter colors correspond to higher pressures)')
	mp.savefig(file_dest)
	mp.clf()




print "#------------------------------------------------#"
print "#        PROCESSING                                #"
print "#------------------------------------------------#"
#mapOfPressure("e327.dat", "pressure_curves_simpson.png", integ.Isimpson, 0.1, 100)
print "#------------------------------------------------#"
print "#        PROCESSING                                #"
print "#------------------------------------------------#"

mapOfPressure("e327.dat", "pressure_curves_trap.png", integ.Itrap, 0.1, 100)
#mapOfPressure("e327.dat", "pressure_curves_midpoint.png", integ.Imidpoint, 0.1, 100)
